package cn.com.ege.mvc.action.business;

import java.io.Serializable;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.com.ege.mvc.controller.BaseAction;
import cn.com.ege.mvc.controller.IAction;
import cn.com.ege.mvc.exception.BusinessException;
import cn.com.ege.mvc.model.business.BomMaterial;
import cn.com.ege.mvc.model.business.ModelRule;
import cn.com.ege.mvc.model.business.Shoe;
import cn.com.ege.mvc.service.impl.business.BomMaterialService;
import cn.com.ege.mvc.service.impl.business.ModelRuleService;
import cn.com.ege.mvc.service.impl.business.ShoeService;
import cn.com.ege.mvc.util.PageController;

public class BomAction extends BaseAction implements IAction, Serializable {
	private final static Log logger = LogFactory.getLog(BomAction.class);
	private PageController pager = null;
	private ShoeService service = null;
	private Shoe shoe = null;
	private List<Shoe> shoeList = null;

	public String execute(HttpServletRequest request,
			HttpServletResponse response, ServletConfig servletConfig) {
		String act = request.getParameter("act");
		if (null != act) {
			// 取列表
			if ("list".equals(act)) {
				int totalRows = service.getRecordCount(new Shoe());
				String URL = request.getRequestURI();
				this.pager.setURL(URL);
				this.pager.setTotalRowsAmount(totalRows);
				shoeList = service.list(new Shoe(), this.pager
						.getPageStartRow(), pager.getPageSize(),
						"order by id desc", true);
				List<ModelRule> modelList = (new ModelRuleService())
						.listAll(new ModelRule());
				request.setAttribute("modelList", modelList);
				pager.setData(shoeList);
				return getReturnURL(LIST);
			}
			// 点了添加或点了修改
			else if ("get".equals(act)) {
				shoe = service.get(shoe);
				List<ModelRule> modelList = (new ModelRuleService())
						.listAll(new ModelRule());
				request.setAttribute("modelList", modelList);
				return getReturnURL(EDIT);
			}
			// 保存表单
			else if ("save".equals(act)) {
				try {
					service.save(shoe);
					return getReturnURL("getList");
				} catch (BusinessException e) {
					// 保存原来表单已输入的内容
					request.setAttribute("shoe", shoe);
					addMessage(e.getMessage());
					return getReturnURL(EDIT);
				}
			}
			// 删除
			else if ("del".equals(act)) {
				String[] ids = request.getParameterValues("id");
				for (String id : ids) {
					Shoe s = new Shoe();
					BomMaterial bm = new BomMaterial();
					if (null != id && !("".equals(id))) {
						bm.setShoe_id(Long.valueOf(id));
						new BomMaterialService().delete(bm);
						s.setId(Long.parseLong(id));
						service.delete(s);
					}
				}
				return getReturnURL("getList");
			}
		}
		return null;
	}

	public PageController getPager() {
		return pager;
	}

	public void setPager(PageController pager) {
		this.pager = pager;
	}

	public ShoeService getService() {
		return service;
	}

	public void setService(ShoeService service) {
		this.service = service;
	}

	public Shoe getShoe() {
		return shoe;
	}

	public void setShoe(Shoe shoe) {
		this.shoe = shoe;
	}

	public List<Shoe> getShoeList() {
		return shoeList;
	}

	public void setShoeList(List<Shoe> shoeList) {
		this.shoeList = shoeList;
	}

}
